#!/usr/bin/env python

import glob, os

os.chdir ( ".." )

def extract_jsdoc ( fname ):
	doc = []
	in_doc = False

	for l in open ( fname ):
		c = l.strip ()
		if c == "/**":
			in_doc = True
			continue
		elif c == "**/":
			in_doc = False
		
		if in_doc: doc.append ( l )
			
	return ''.join ( doc )

def extract_js ( dct ):
	
	page = [
		"=" * 100,
		dct [ 'title' ],
		"=" * 100,
		".. toctree::",
		"\t:maxdepth: 2",
		"",	
		"",
	]
		
	for f in glob.glob ( "%(dir)s/*.js" % dct ):
		if f.endswith ( ".comp.js" ): continue

		data = extract_jsdoc ( f ) 

		if data:
			outf = os.path.basename ( f ).replace ( ".js", ".txt" )
			dct [ 'outf' ] = outf
			open ( "docs/source/%(dir)s/%(outf)s" % dct, "w" ).write ( data )

			page.append ( "\t%s" % outf )

	open ( "docs/source/%(dir)s/index.txt" % dct, "w" ).write ( '\n'.join ( page ) )


extract_js ( { "dir" : "os3jslib", "title" : "JS Lib" } )
